#include <dirent.h>
#include "server_module.h"

// 管理员及普通用户登陆
int login_in(int fd, struct login_inform *logininfo, sqlite3 *db)
{
	int ret;
	char buf[128] = {0};
	char *errmsg;
	char **azresult;
	int nrow;
	int ncolumn;
// 接受登陆信息
	if((ret = recv( fd, logininfo, sizeof(struct login_inform), 0)) < 0)
	{
		printf("fail to recv login inform\n");
		ret = -1;						// 接受登陆信息失败
		send(fd, &ret, 4, 0);
		return 1;
	}else if(ret == 0){
		printf("client exit not normal\n");	// 客户端异常退出
		return -1;
	}
	if(strcmp(logininfo->login_user, "quit") == 0)
	{
		printf("client exit\n");	// 客户端正常退出
		return -1;
	}
	// 拼接SQL命令
	sprintf(buf, "select * from staff where name = '%s' and passwd = '%s'", logininfo->login_user, logininfo->login_passwd);
	if(sqlite3_get_table(db, buf, &azresult, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
	{
		printf("%s\n", errmsg);
		ret = -1;
		send(fd, &ret, 4, 0);		// 查询数据库失败
		return 1;
	}
	if(nrow > 0)	// 登陆信息匹配
	{
		if (strcmp(logininfo->login_user, "root") == 0)		//root用户登陆
		{
			printf("root login in\n");
			ret = 1;
			send(fd, &ret, 4, 0);
			return 0;
		}else{
			printf("common user %s login in\n", logininfo->login_user);		//普通用户登陆
			ret = 2;
			send(fd, &ret, 4, 0);
			return 0;
		}
	}else{
		printf("wrong user name or password\n"); // 登陆信息不匹配
		ret = -1;
		send(fd, &ret, 4, 0);
		return 1;
	}
	
}

// 用户信息存储结构体
struct user_info {
	char user_name[32];                  // 专门存放用户名字
	char user_passwd[32];                // 专门存放用户密码
	char user_phone[32];                 // 专门存放用户手机
	char user_address[32];               // 专门存放用户地址    
}; 
// 增加用户
void add_user(int fd, sqlite3 *db)
{
	int ret;
	char buf[128] = {0};
	char *errmsg;
	struct user_info userinfo;

	recv(fd, &userinfo, sizeof(userinfo), 0);	// 接受用户信息
	//拼接sql命令
	sprintf(buf, "insert into staff values('%s', '%s', '%s', '%s')", userinfo.user_name, userinfo.user_passwd, userinfo.user_phone, userinfo.user_address);

	if(sqlite3_exec(db, buf, NULL, NULL, &errmsg) != SQLITE_OK)
	{
		printf("add user failed\n");	// 添加失败
		ret = -1;
		send(fd, &ret, 4, 0);
		return;
	}else{
		printf("add user success\n");	// 添加成功
		ret = 0;
		send(fd, &ret, 4, 0);
		return;
	}

}
// 查询用户
void query_user(int fd, sqlite3 *db)
{
	char buf[128] = {0};
	char **azresult;
	int nrow;
	int ncolumn;
	char *errmsg;
	struct user_info userinfo;

	recv(fd, &userinfo, sizeof(userinfo), 0);	// 接受用户信息
	// 拼接sql命令
	sprintf(buf, "select * from staff where name = '%s'", userinfo.user_name);

	if(sqlite3_get_table(db, buf, &azresult, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
	{
		printf("errmsg :%s\n", errmsg);			// 查询数据库失败
		strcpy(userinfo.user_name, "wrong");
		send(fd, &userinfo, sizeof(userinfo), 0);
		return;
	}
	if(nrow > 0)			// 查询有结果
	{
		printf("query success\n");
		// 拷贝查询结果
		strcpy(userinfo.user_passwd, azresult[nrow*ncolumn+1]);
		strcpy(userinfo.user_phone, azresult[nrow*ncolumn+2]);
		strcpy(userinfo.user_address, azresult[nrow*ncolumn+3]);
		printf("name:%s password:%s phone:%s addr:%s\n", userinfo.user_name, userinfo.user_passwd, userinfo.user_phone, userinfo.user_address);
		send(fd, &userinfo, sizeof(userinfo), 0);	// 发送查询结果
	}else{
		printf("no user\n");	// 查询没结果
		strcpy(userinfo.user_name, "wrong");
		send(fd, &userinfo, sizeof(userinfo), 0);
	}
	sqlite3_free_table(azresult);	// 释放查询时的空间
	return;
}
// 删除用户
void del_user(int fd, sqlite3 *db)
{
	int ret;
	char *errmsg;
	char buf[128];
	struct user_info userinfo;

	recv(fd, &userinfo, sizeof(userinfo), 0);	// 接受用户信息
	// 拼接sql命令
	sprintf(buf, "delete from staff where name = '%s'", userinfo.user_name);
	if(sqlite3_exec(db, buf, NULL, NULL, &errmsg) != SQLITE_OK)
	{
		printf("errmsg:%s\n", errmsg);		// 删除失败
		ret = -1;
	}else{
		printf("del success\n");		// 删除成功
		ret = 0;
	}
	send(fd, &ret, 4, 0);	// 发送反馈
}
// 更新用户
void update_user(int fd, sqlite3 *db)
{
	int ret;
	char buf[128];
	char *errmsg;
	struct user_info userinfo;

	recv(fd, &userinfo, sizeof(userinfo), 0);	// 接受用户信息
	// 拼接sql命令
	sprintf(buf, "update staff set passwd = '%s' where name = '%s'", userinfo.user_passwd, userinfo.user_name);

	if(sqlite3_exec(db, buf, NULL, NULL, &errmsg) != SQLITE_OK)
	{
		printf("fail to update\n");		//更新成功
		ret = -1;
	}else{
		printf("update success\n");		//更新失败
		ret = 0;
	}
	send(fd, &ret, 4, 0);			//发送反馈信息
}

// 普通用户查询指定用户特定信息
void user_query_other_people(int fd, sqlite3 *db)
{
	char buf[128] = {0};
	char **azresult;
	int nrow;
	int ncolumn;
	char *errmsg;
	struct user_info userinfo;

	recv(fd, &userinfo, sizeof(userinfo), 0);	// 接受用户信息
	// 拼接sql命令
	sprintf(buf, "select * from staff where name = '%s'", userinfo.user_name);

	if(sqlite3_get_table(db, buf, &azresult, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
	{
		printf("errmsg :%s\n", errmsg);			// 查询失败
		strcpy(userinfo.user_name, "wrong");
		send(fd, &userinfo, sizeof(userinfo), 0);
		return;
	}
	if(nrow > 0)	// 查询有结果
	{
		printf("query success\n");
		printf("nrow:%d, ncolumn:%d\n", nrow, ncolumn);
		// 拷贝查询结果
		strcpy(userinfo.user_passwd, "*********");
		strcpy(userinfo.user_phone, azresult[nrow*ncolumn+2]);
		strcpy(userinfo.user_address,azresult[nrow*ncolumn+3]);
		printf("name:%s password:%s phone:%s addr:%s\n", userinfo.user_name, userinfo.user_passwd, userinfo.user_phone, userinfo.user_address);
		send(fd, &userinfo, sizeof(userinfo), 0);	// 发送查询结果
	}else{
		printf("no user\n");	// 查询没结果
		strcpy(userinfo.user_name, "wrong");
		send(fd, &userinfo, sizeof(userinfo), 0);
	}
	sqlite3_free_table(azresult);	// 释放查询空间
	return;
}
// 显示服务器文件
void list_file(int fd)
{
	DIR *dir;
	char buf[256] = {0};
	struct dirent *dirent;
	// 打开文件目录
	if((dir = opendir("./docm/")) == NULL)
	{
		strcpy(buf, "fail");
		send(fd, buf, 128, 0);
		perror("fail to open dir:");
		return;
	}
	// 读取文件目录
	while((dirent = readdir(dir)) != NULL)
	{
		printf("d_name:%s\n", dirent->d_name);
		if((!strcmp(dirent->d_name, "."))||(!strcmp(dirent->d_name, "..")))	// 规避当前目录及上级目录
		{
			continue;
		}
		send(fd, &(dirent->d_name), 256, 0);	// 发送目录信息
	}

	send(fd, "over", 256, 0);	// 发送完毕
	printf("list over\n");
}
// 向服务器上传文件
void put_file(int fd)
{
	int f_fd;
	int ret;
	char buf[128] = {0};
	char filename[128] = {0};
	char filepath[200] = {0};

	recv(fd, filename, 128, 0);	// 接受客户端文件名

	if(strcmp(filename, "fail") == 0)
	{
		printf("file is invalid\n");
		return;
	}
	sprintf(filepath, "./docm/%s", filename);	// 拼接文件路径

	f_fd = open(filepath, O_RDWR|O_CREAT, 0777);	// 创建文件等待接受
	if(f_fd < 0)
	{
		perror("fail to open file:\n");
		send(fd, "fail", 128, 0);
		return;
	}
	send(fd, "ok", 128, 0);

	while((ret = recv(fd, buf, 128, 0)) > 0) // 接受客户端信息
	{
		write(f_fd, buf, ret);
		if(ret < 128)					// 接受到结尾
		{
			break;
		}
	}
	printf("put over\n");
	
}
// 从服务器上下载文件
void load_file(int fd)
{
	int f_fd;
	int ret;
	char buf[128] = {0};
	char filename[128] = {0};
	char filepath[200] = {0};

	recv(fd, filename, 128, 0);		// 接受客户端文件名
	sprintf(filepath, "./docm/%s", filename);	// 拼接文件路径
	f_fd = open(filepath, O_RDONLY);	// 打开服务器端文件
	if(f_fd < 0)
	{
		send(fd, "fail", 128, 0);
		perror("fail to open file:");
		return;
	}
	send(fd, "ok", 128, 0);
	
	recv(fd, buf, 128, 0);		// 接受客户端能否接受的反馈信息
	if(strcmp(buf, "fail") == 0)
	{
		printf("fail to load fiel\n");
		return;
	}

	while((ret = read(f_fd, buf, 128)) > 0) // 读取服务器端文件信息
	{
		if(ret < 128)					// 读取文件到结尾
		{
			send(fd, buf, ret, 0);
			continue;
		}
		send(fd, buf, 128, 0);			// 发送文件信息
	}
	printf("load over\n");

}
